package com.urerp.web.view;

import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.servlet.view.InternalResourceView;

import com.urerp.web.common.CommonUtil;

public class JstlView extends InternalResourceView  {
	
	private final static Logger logger = LogManager.getLogger(JstlView.class);
	
	@Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
        String dispatcherPath = prepareForRendering(request, response);
        
        logger.debug("DispatcherPath : " + dispatcherPath + " | RequestURI : " + request.getRequestURI());
        
        // set original view being asked for as a request parameter
        request.setAttribute("partial", dispatcherPath);
        
        // Expose the model object as request attributes.
        exposeModelAsRequestAttributes(model,request);
        
        String layout = CommonUtil.rNull(request.getParameter("layout"), "Y");
        
        // force everything to be template.jsp
        RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/template.jsp");
        
        if (dispatcherPath.equals("/WEB-INF/views/login/login.jsp")) {
        	
        	rd = request.getRequestDispatcher("/WEB-INF/views/template_login.jsp");
        	
        } else if (layout.equals("N")) {
        	
        	rd = request.getRequestDispatcher(dispatcherPath);
        	
        }
        
    	rd.include(request, response);
        
    }
	
	@Override
    public boolean isUrlRequired() {
        return false;
    }
}
